package com.mifengkong.frwidget.popup_window.base;

import android.animation.Animator;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.FrameLayout;

import com.mifengkong.frwidget.R;


/**
 * 从底部滑上来的popup
 */
public class SlideFromBottomBasePopupMenu extends BasePopupWindow {

    public SlideFromBottomBasePopupMenu(Activity context) {
        super(context);
    }

    @Override
    protected final View getInputView() {
        return null;
    }

    @Override
    public void showPopupWindow(View v) {
        //适配Android8.0
        if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            v.getGlobalVisibleRect(visibleFrame);
            int height = v.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            int[] location = new int[2];
            v.getLocationOnScreen(location);
            mPopupWindow.setHeight(height);
            mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, 0, location[1] + v.getHeight());
        } else {
            mPopupWindow.showAsDropDown(v, 0, 0);
        }
    }

    @Override
    protected final Animator getAnimator() {
        return null;
    }

    @Override
    protected final Animation getAnimation() {
        return getTranslateAnimation(1000 * 2, 0, 300);
    }

    @Override
    public final View getPopupView() {
        return mContext.getLayoutInflater().inflate(R.layout.pw_slidefrombottom_popupmenu, null);
    }

    @Override
    protected final View getDismissView() {
        return mPopupView.findViewById(R.id.click_to_dismiss);
    }

    @Override
    protected Animation getExitAnimation() {
        return getTranslateAnimation(0, 1000 * 2, 300);
    }

    @Override
    protected Animator getExitAnimator() {
        return null;
    }

    @Override
    public final View getAnimationView() {
        return mPopupView.findViewById(R.id.popup_anima);
    }

    public void setMenuItems(View itemsView) {
        FrameLayout itemContainer = (FrameLayout) mPopupView.findViewById(R.id.item_container);
        itemContainer.removeAllViews();
        itemContainer.addView(itemsView);
    }
}
